﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Compilex.Automata;
using Compilex.BaseLibrary;

namespace Compilex.Automata
{
    public class TokenRecognizer : LongestPrefixRecognizer
    {
        public delegate Token GetTokenDelegate(SimpleHashtable<string, WordToken> words, string input);
        private GetTokenDelegate _GetToken;
        private string lastRecognizedInput = null;

        public TokenRecognizer(DFA automaton, GetTokenDelegate deleg)
            : base(automaton)
        {
            this._GetToken = deleg;
        }

        public override int TryRecognizeLongestPrefix(string fullInput)
        {
            int maxLen = base.TryRecognizeLongestPrefix(fullInput);
            if (maxLen > 0)
            {
                lastRecognizedInput = fullInput.Substring(0, maxLen);
            }
            return maxLen;
        }

        public virtual Token GetLastToken(SimpleHashtable<string, WordToken> words)
        {
            ThrowHelper.ThrowIfNull(lastRecognizedInput);
            return this._GetToken(words, lastRecognizedInput);
        }



    }
}
